﻿//////////////////////////////////////////////////////////////////////////
// FileName:fmCarMaintenanceForm.aspx
// CreateBy:DragonXiong
// CreateDate:2010-05-22
// Description:
//		Car Maintenance Form
//      
// Modify Log:
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
using BLL = Auto.Data.BusMan.BLL;

namespace Auto.BusMan
{
    public partial class fmCarChangeTyreManager : System.Web.UI.Page
    {
        public string DocID
        {
            get
            {
                ViewState["DocID"] = Request.QueryString["DocID"];
                return (ViewState["DocID"] == null) ? "" : ViewState["DocID"].ToString();
            }
            set { ViewState["DocID"] = value; }
        }

        Model.BusTyreManage model = new Model.BusTyreManage();
        BLL.BusTyreManage bll = new BLL.BusTyreManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            if (string.IsNullOrEmpty(DocID)) //新增
            {
                this.chkAddContinue.Checked = true;
                checktr.Visible = true;
                checktr.Attributes.Add("style", "display:block;");
                InitControl();
            }
            else  //修改
            {
                checktr.Attributes.Add("style", "display:none;");
                BindData();
            }
        }
        private void InitControl()
        {
            this.ucDDLCarSelector1.InitControl();
        }

        private void ClearText()
        {
            //this.txtBusID.Text=model.BusID;
            this.ucDDLCarSelector1.SelectedValue = "";// this.txtBusIdentificationNO.Text = "";// model.BusIdentificationNO;
            this.txtOdometerBus.Text = "";// model.OdometerBus.ToString();
            this.txtTyreBrand.Text = "";// model.TyreBrand;
            this.txtChangeDate.Text = "";// model.ChangeDate.ToString();
            this.txtChangeNO.Text = "";// model.ChangeNO.ToString();
            this.txtTyrePrice.Text = "";//model.TyrePrice.ToString();
            this.ddlChangeLocation.SelectedValue = "";// this.txtChangeLocation.Text = "";//model.ChangeLocation;
            this.txtChangeReason.Text = "";// model.ChangeReason;
            //this.txtNote.Text=model.Note;
        }

        private void BindData()
        {
            InitControl();
            model = bll.GetModel(DocID);
            Session["fmCarChangeTyreManager.Model"] = model;
            //this.txtBusID.Text=model.BusID;
            this.ucDDLCarSelector1.SelectedValue = model.BusID;// this.txtBusIdentificationNO.Text = model.BusIdentificationNO;
            this.txtOdometerBus.Text = model.OdometerBus.ToString();
            this.txtTyreBrand.Text = model.TyreBrand;
            this.txtChangeDate.CalendarDate = model.ChangeDate;
            this.txtChangeNO.Text = model.ChangeNO.ToString();
            this.txtTyrePrice.Text = model.TyrePrice.ToString();
            ListItem item = ddlChangeLocation.Items.FindByText(model.ChangeLocation);
            if (item != null)
            {
                ddlChangeLocation.ClearSelection();
                item.Selected = true;
            }
            
            this.txtChangeReason.Text = model.ChangeReason;
            //this.txtNote.Text=model.Note;
        }


        private Model.BusTyreManage GetBusTyreManage()
        {
            if (!string.IsNullOrEmpty(DocID))
            {
                model = (Model.BusTyreManage)Session["fmCarChangeTyreManager.Model"];
            }
            //Tab1
            string BusID = this.ucDDLCarSelector1.SelectedValue;// this.txtBusID.Text;
            string BusIdentificationNO = this.ucDDLCarSelector1.BusIdentificationNO;// this.txtBusIdentificationNO.Text;
            decimal OdometerBus = decimal.Parse(this.txtOdometerBus.Text);
            string TyreBrand = this.txtTyreBrand.Text;
            DateTime ChangeDate = DateTime.Parse(this.txtChangeDate.Text);
            int ChangeNO = int.Parse(this.txtChangeNO.Text);
            decimal TyrePrice = decimal.Parse(this.txtTyrePrice.Text);
            string ChangeLocation = this.ddlChangeLocation.SelectedItem.Text;// this.txtChangeLocation.Text;
            string ChangeReason = this.txtChangeReason.Text;
            string Note = "";// this.txtNote.Text;

            //Tab1
            model.BusID = BusID;
            model.BusIdentificationNO = BusIdentificationNO;
            model.OdometerBus = OdometerBus;
            model.TyreBrand = TyreBrand;
            model.ChangeDate = ChangeDate;
            model.ChangeNO = ChangeNO;
            model.TyrePrice = TyrePrice;
            model.ChangeLocation = ChangeLocation;
            model.ChangeReason = ChangeReason;
            model.Note = Note;

            return model;


        }

        private bool Check()
        {
            string strErr = "";

            if (ucDDLCarSelector1.SelectedValue == "")
            {
                strErr += "请选择车辆!\\n";
            }
            if (strErr != "")
            {
                Jscript.AjaxAlert(this, strErr);
                return false;
            }
            else
            {
                return true;
            }
        }

        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Check())
            {
                return;
            }      
            try
            {
                model = GetBusTyreManage();
                model.RecDivID = SessionBox.User.DivID;
                model.UserAcct = SessionBox.User.UserID;
                if (string.IsNullOrEmpty(DocID))
                {
                    bll.Add(model);
                }
                else
                {
                    bll.Update(model);
                }
                Redirect();

            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this, ex.Message);
            }

        }
        private void Redirect()
        {
            if (chkAddContinue.Checked)
            {
                lblMsg.Text = "保存成功，请继续添加！";
                ClearText();
            }
            else
            {
                Server.Transfer("fmCarChangeTyreList.aspx");
            }
        }

        #endregion Event
    }
}